package net.slenderloris.TouchServer;

import java.awt.Point;
import java.util.HashMap;

public class GestureEventGenerator {

	public interface OnTapListener {
		public void onTap(int x, int y);
	}
	
	private static class FingerTracker {
		private Point mDownPos;
		private int mDownTime;
	}

	private HashMap<Integer, FingerTracker> mFingers = new HashMap<Integer, GestureEventGenerator.FingerTracker>();

	private OnTapListener mOnTapListener;
	
	public static final int MAX_SLOP_DISTANCE_SQ = 100;
	public static final int MAX_SLOP_TIME = 200;
	
	public void setOnTapListener(OnTapListener onTapListener) {
		mOnTapListener = onTapListener;
	}
	
	public void fingerDown(int id, int time, int x, int y) {
		FingerTracker tracker = mFingers.get(id);
		if(tracker==null) {
			tracker = new FingerTracker();
			tracker.mDownPos = new Point(x,y);
			mFingers.put(id, tracker);
		} else {
			tracker.mDownPos.x = x;
			tracker.mDownPos.y = y;
		}
		tracker.mDownTime = time;
	}
	
	public void fingerUp(int id, int time, int x, int y) {
		FingerTracker tracker = mFingers.get(id);
		if(tracker!=null) {
			int dt = time-tracker.mDownTime;
			if(dt<MAX_SLOP_TIME) {
				int dx = x-tracker.mDownPos.x;
				int dy = y-tracker.mDownPos.y;
				int distSq = (dx*dx)+(dy*dy);
				if(distSq<MAX_SLOP_DISTANCE_SQ) {
					if(mOnTapListener!=null) {
						mOnTapListener.onTap(x, y);
					}
				}
			}
		}
	}
	
	
	
}
